package com.tcr.dao;

import java.sql.Connection;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.tcr.common.TcUser;

public class DbUtil {

	private DbUtil() {
		// ConnectionPool Size = 20 defalut;
		pool = new ConnectionPool(20);
	}

	public synchronized static DbUtil getInstance() {
		if (dbutil == null)
			dbutil = new DbUtil();
		return dbutil;
	}

	public void init() {
		Connection conn = null;
		try {
			conn = pool.getConnection();
			conn.setAutoCommit(false);
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(SqlString.crt_tc_user);
			stmt.executeUpdate(SqlString.crt_tc_proj);
			stmt.executeUpdate(SqlString.crt_tc_upmap);
			stmt.executeUpdate(SqlString.crt_tc_cov);

			conn.commit();

			logInit();

		} catch (Exception ex) {
			logger.error(ex);
		} finally {
			if (conn != null)
				pool.backConn(conn);
		}
	}

	void logInit() {
		logger.info("Create table tc_user");
		logger.info("Create table tc_proj");
		logger.info("Create table tc_upmap");
		logger.info("Create table tc_cov");
	}

	private static DbUtil dbutil = null;
	public ConnectionPool pool = null;
	private final static Logger logger = Logger.getLogger(DbUtil.class
			.getName());

	//Test
	public static void main(String[] args){
		DbUtil dbutil = DbUtil.getInstance();
		boolean b;
		b = CoverageDao.add_cov(4, "dfe","dfdf"	, 4);
		System.out.println(b);
		b = CoverageDao.add_cov(4, "dfe","dfdf"	, 4);
		System.out.println(b);
		int size = CoverageDao.getCovByPid(4).size();
		System.out.println(size);
	}
}
